package command

import (
	"ssdevice/common/dto"
	"ssdevice/common/utils"
	"ssdevice/internal/device/models"
	"strings"
	"time"
)

type PackageInsertReq struct {
	Name         string `json:"name"`
	Version      string `json:"version" vd:"len($)>0"`
	ProductId    string `json:"productId" vd:"len($)>0"`
	ProductName  string `json:"productName" comment:"分组id"`
	PackagePath  string `json:"packagePath" comment:"产品图片"`
	Remark       string `json:"remark" comment:"产品描述"`
	dto.UserInfo `search:"-"`
}

func (d *PackageInsertReq) Generate(model *models.SysOTAPackage) error {

	model.Id = utils.SimpleUUID()
	model.Name = strings.TrimSpace(d.Name)
	model.Version = d.Version
	model.ProductId = d.ProductId
	model.ProductName = d.ProductName
	model.PackagePath = d.PackagePath
	model.Remark = d.Remark
	now := dto.LocalTime(time.Now())
	model.CreateTime = &now
	model.UpdateTime = &now
	model.CreateBy = d.UserInfo.UserName
	model.UpdateBy = d.UserInfo.UserName
	return nil
}

type PackageUpdateReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	Name         string `json:"name"`
	Version      string `json:"version" vd:"len($)>0"`
	ProductId    string `json:"productId" vd:"len($)>0"`
	ProductName  string `json:"productName" comment:"分组id"`
	PackagePath  string `json:"packagePath" comment:"产品图片"`
	Remark       string `json:"remark" comment:"产品描述"`
	dto.UserInfo `search:"-"`
}

func (d *PackageUpdateReq) Generate(model *models.SysOTAPackage) error {

	model.Id = d.Id
	model.Name = strings.TrimSpace(d.Name)
	model.Version = d.Version
	model.ProductId = d.ProductId
	model.ProductName = d.ProductName
	model.PackagePath = d.PackagePath
	model.Remark = d.Remark
	now := dto.LocalTime(time.Now())
	model.UpdateTime = &now
	model.UpdateBy = d.UserInfo.UserName

	return nil
}

type PackageDeleteReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}

type BatchTaskInsertReq struct {
	Name         string                 `json:"name"`
	Status       int                    `json:"status" `
	UpdateType   string                 `json:"updateType" comment:"子设备类型"`
	PackageId    string                 `json:"packageId" comment:"安装包id"`
	ProductId    string                 `json:"productId" `
	ConfigData   map[string]interface{} `json:"configData" comment:"配置数据"`
	TaskType     string                 `json:"taskType" comment:"任务类型"`
	Filters      map[string]interface{} `json:"filters" comment:"过滤条件"`
	dto.UserInfo `search:"-"`
}

func (d *BatchTaskInsertReq) Generate(model *models.SysBatchTask) error {

	model.Id = utils.SimpleUUID()
	model.Name = strings.TrimSpace(d.Name)
	model.Status = d.Status
	model.UpdateType = d.UpdateType
	model.PackageId = d.PackageId
	model.ProductId = d.ProductId
	model.ConfigData = utils.MapToBytes(utils.If(len(d.ConfigData) > 0, d.ConfigData, map[string]interface{}{}).(map[string]interface{}))
	model.TaskType = d.TaskType
	model.Filters = utils.MapToBytes(utils.If(len(d.Filters) > 0, d.Filters, map[string]interface{}{}).(map[string]interface{}))
	now := dto.LocalTime(time.Now())
	model.CreateTime = &now
	model.UpdateTime = &now
	model.CreateBy = d.UserInfo.UserName
	return nil
}

type BatchTaskUpdateReq struct {
	Id           string                 `json:"id"`
	Name         string                 `json:"name"`
	Status       int                    `json:"status" `
	UpdateType   string                 `json:"updateType" comment:"子设备类型"`
	PackageId    string                 `json:"packageId" comment:"安装包id"`
	ProductId    string                 `json:"productId" `
	ConfigData   map[string]interface{} `json:"configData" comment:"配置数据"`
	TaskType     string                 `json:"taskType" comment:"任务类型"`
	Filters      map[string]interface{} `json:"filters" comment:"过滤条件"`
	dto.UserInfo `search:"-"`
}

func (d *BatchTaskUpdateReq) Generate(model *models.SysBatchTask) error {

	model.Id = d.Id
	model.Name = strings.TrimSpace(d.Name)
	model.Status = d.Status
	model.UpdateType = d.UpdateType
	model.PackageId = d.PackageId
	model.ProductId = d.ProductId
	model.ConfigData = utils.MapToBytes(utils.If(len(d.ConfigData) > 0, d.ConfigData, map[string]interface{}{}).(map[string]interface{}))
	model.TaskType = d.TaskType
	model.Filters = utils.MapToBytes(utils.If(len(d.Filters) > 0, d.Filters, map[string]interface{}{}).(map[string]interface{}))
	now := dto.LocalTime(time.Now())
	model.UpdateTime = &now
	return nil
}

type BatchTaskDeleteReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}

type BatchTaskExecReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}
